from php4dvd.pages.page import Page
#from selenium.webdriver.support.select import Select
from selenium.common.exceptions import NoSuchElementException

class FilmForm(Page):

    def is_element_present(self, elem, what):
        try:
            elem.find_element_by_css_selector(what)
        except NoSuchElementException, e:
            return False
        return True

    @property
    def main_table(self):
        return self.driver.find_element_by_xpath(
            "//*[@id=\"updateform\"]/table")

    @property
    def save_button(self):
        return self.driver.find_element_by_id("submit")

    @property
    def name_editbox(self):
        return self.main_table.find_element_by_css_selector(
            "input[name=\"name\"]")

    @property
    def year_editbox(self):
        return self.main_table.find_element_by_css_selector(
            "input[name=\"year\"]")

    def data_warnings_num(self):
        num = 0
        for element in self.main_table.find_elements_by_css_selector(
            "label[class=\"error\"]"):
            if element.is_displayed():
                num = num + 1
                print "=>Can`t add film - we have warning for [" + (
                    element.get_attribute("for")
                    ) +  "]: \"" + element.text + "\""
        return num

